package servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import service.AdminService;

@WebServlet("/AdminServlet")
public class AdminServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    public AdminServlet() {
        super();
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		AdminService service = new AdminService();
		String operation = request.getParameter("operation");
		String page = request.getParameter("page");
		String name = request.getParameter("name");
		String id = request.getParameter("id");
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		String lock = request.getParameter("lock");
		
		page = page == null ? "1" : page;
		
		switch(operation) {
		case "getall" : 
			response.getWriter().write(service.getAllJson(page, name, id));break;
		case "add" : 
			service.addAdmin(username, password);break;
		case "alt" :
			service.altAdmin(id, username, password);break;
		case "lock" :
			service.lockAdmin(id, lock);break;
		case "del" : 
			service.delAdmin(id);break;
		}
	}

}
